/**
 * 
 */
package cn.ac.istic.lkt.atv;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.image.BufferedImage;
import java.util.List;

import javax.swing.JPanel;

/**
 * @author Chongde Shi
 *
 */
public abstract class TreePanelAbstract extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8063912666564470434L;
	public static final Font TFONT = new Font("Serif", Font.PLAIN, 16);
	//public static final Font NTFONT = new Font("Serif", Font.PLAIN, 14);
	public static final BufferedImage tempbi = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
	public static final float TOPY = 30; // 和顶上的距离
	public static final float LINEHEIGHT = 30; // 两个字符的垂直举例
	public static final float STARTX = 30;
	public static final float wordSpace = 30; // 单词间的间隔
	
	protected float charHeight; // 字符高度
	protected float charWidth; // 字符宽
	protected int imgWidth = 0;
	protected int imgHeight = 0;

	TreePanelAbstract(){
		imgWidth = 0;
		imgHeight = 0;
		//setBackground(Color.WHITE);
	}
	
	public BufferedImage paintImage(){		
		FontRenderContext frc = tempbi.createGraphics().getFontRenderContext();
		charHeight = (float) TFONT.getMaxCharBounds(frc).getHeight();
		charWidth =((float) TFONT.getMaxCharBounds(frc).getWidth());
		//des2node = charHeight + LINEHEIGHT;
		calcPos(frc );
		
		//System.out.println("TreePanelAbstract: "+imgWidth+" "+imgHeight);
		BufferedImage bi = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2 = bi.createGraphics();
		myPaint(g2);
		return bi;
	}
	
	public abstract void calcPos(FontRenderContext frc);
	public abstract void myPaint(Graphics2D g2);
	//public abstract void init(ParseTree pt);
	//public abstract void init(ParseTree pt1,ParseTree pt2);
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;

		myPaint(g2);
		setPreferredSize(new Dimension(imgWidth,imgHeight));
		revalidate();
		//System.err.println(maxX + this.STARTX+" "+maxY + this.STARTX);
		// updateUI();
	}

	
	public void drawLines(Graphics2D g2, ParseTree pt, PTNode tn ) {		
		List<PTNode> nodelist = pt.getNodeList();
		if (tn.isWord()) {
			;
		} else {
			int[] child = pt.getChildren(tn.getID());
			for (int i = 0; i < child.length; i++) {
				PTNode childnd = nodelist.get(child[i]);
				Shape line = new java.awt.geom.Line2D.Double( tn.getXmiddle(),
						tn.getYdown(), childnd.getXmiddle(), childnd.getYup());
				g2.draw(line);				
				drawLines(g2,pt,childnd);
			}
		}
	}
	public int getImgWidth() {
		return imgWidth;
	}

	public void setImgWidth(int imgWidth) {
		this.imgWidth = imgWidth;
	}

	public int getImgHeight() {
		return imgHeight;
	}

	public void setImgHeight(int imgHeight) {
		this.imgHeight = imgHeight;
	}
}
